import os
import pandas as pd
import numpy as np
from quant_researcher.quant.datasource_fetch.crypto_api.coinmarketcap import get_asset_ohlcvm_via_http
from quant_researcher.quant.project_tool.time_tool import date_shifter, get_yesterday
from quant_researcher.quant.datasource_fetch.crypto_api.glassnode import get_prices
from utils import df_into_db


start_date = '2013-01-01'
end_date = get_yesterday(marker='with_n_dash')  # 计算截止昨日收盘
for asset in ["bitcoin", "ethereum"]:
    DATA_DIR = f'E:\\指标数据'  # 用于存放测试数据
    file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap\all_history_ohlcvm_coinmarketcap')
    file_name = os.path.join(file_path, f'{asset}')

    history_ohlcvm = pd.read_excel(f'{file_name}.xlsx', index_col='end_date')
    if history_ohlcvm.empty:
        history_ohlcvm_latest_date = '2013-01-01'
    else:
        history_ohlcvm_latest_date = history_ohlcvm.index[-1]

    all_ohlcvm = get_asset_ohlcvm_via_http(asset=asset, range='1Y')
    # 数据合并
    next_date = date_shifter(history_ohlcvm_latest_date, step='days', how_many=1)
    if all_ohlcvm.index[0] > next_date:
        print(f'读取的最新数据跟历史数据接不上，请检查数据源是否有问题')
        raise ValueError
    asset_df = pd.concat([history_ohlcvm, all_ohlcvm.loc[next_date:, ]])
    asset_df = asset_df[history_ohlcvm.columns]
    asset_df.sort_index(inplace=True)
    asset_df.index.name = 'end_date'

    try:
        # ohlc的数据还是以glassnode的数据为准
        file_path_1 = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
        file_name_1 = os.path.join(file_path_1, f'all_coin_list_coinmarketcap')
        all_coin_list_df = pd.read_excel(f'{file_name_1}.xlsx', index_col='slug')
        symbol = all_coin_list_df.loc[asset, 'symbol']
        prices_df = get_prices(ohlc=True, asset=f'{symbol}', start_date=start_date, end_date=end_date)
    except Exception as err:  # ohlc 数据获取失败
        prices_df = pd.DataFrame(np.NAN, columns=['open', 'high', 'low', 'close'], index=asset_df.index)

    all_df = pd.concat([asset_df[['market_cap', 'amount', 'volume']], prices_df], axis=1)
    # all_df.dropna(axis=0, inplace=True)
    all_df.sort_index(ascending=True, inplace=True)
    all_df.index.name = 'end_date'
    all_df = all_df.reset_index(drop=False)
    all_df.fillna(0, inplace=True)
    all_df['symbol'] = symbol
    df_into_db(all_df, db_name="all_history_ohlcvm_coinmarketcap", table_name="btc_eth_history_ohlcvm")
